var prmoseHttp = require('../../utils/prmoseHttp.js');

Page({
  data: {
    date: '',
    nav:['全部','签到','广告','邀请','兑换'],
    isSel:0,
    currentPage:1,
    pageSize:10,
    total:0,
    List:[],
    selDate:'',
    imgSrc:'https://rhi0.xiyun.net/zhuandian/image/',
    userId:'',
    loadingFalg:false,
  },
  onLoad(options) {
    
    let date = new Date()
    let y = date.getFullYear();
    let m = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : '0' + (date.getMonth() + 1);
    // let d = date.getDate() > 9 ? date.getDate() : '0' + date.getDate();
    this.setData({
      date:y + '-' + m,
      selDate:y + '-' + m,
    })
    if(options.type){
      this.setData({
        isSel:options.type
      })
      this.getRecord();
    }else{
      this.setData({
        isSel:0
      })
      this.getRecord();
    }
    if(my.getStorageSync({key:'userInfo'}).data.id){
      let id = my.getStorageSync({key:'userInfo'}).data.id;
      this.setData({
        userId:id,
      })
    }
  },
  selNav(e){
    this.setData({
      isSel:e.currentTarget.dataset.index,
      List:[],
      currentPage:1,
      pageSize:10,
    })
    this.getRecord();
  },
  getRecord(){
    let that = this;
    my.showLoading();
    let code = '';
    if(that.data.isSel == 0){
      code = ''
    }else{
      code = that.data.isSel
    }
    prmoseHttp.publicRequest(prmoseHttp.apiAddress.coinRecord,{
      code:code,
      currentPage:that.data.currentPage,
      pageSize:that.data.pageSize,
      time:that.data.selDate+'-01',
    },1,'get').then(res=>{
      if(res.code == 200){
        my.hideLoading();
        that.setData({
          List:that.data.List.concat(res.data.pageInfo.records),
          total:res.data.total,
          loadingFalg:false,
        })
      }
    }).catch(err=>{
      my.hideLoading();
    })
  },
  datePicker() {
    my.datePicker({
      format: 'yyyy-MM',
      currentDate: this.data.date,
      startDate: '2001-01',
      endDate: this.data.date,
      success: (res) => {
        this.setData({
          selDate: res.date,
          List:[]
        });
        this.getRecord();
      },
    })
  },
  // 页面被拉到底部
  onReachBottom() {
    let that = this;
    if (that.data.List.length < that.data.total){
      that.data.currentPage ++;
      that.setData({
        loadingFalg:true,
      });
      that.getRecord();
    }
  },
  onShareAppMessage () {
    return {
      title: '挣点', // 标题
      desc: '任务多多，天天领取现金红包', // 描述
      path: 'pages/index/index?userid='+this.data.userId, // 分享的小程序页面
      success(res){
        my.navigateTo({
          url: '/pages/shareSuccess/shareSuccess'
        }); 
      },
      fail(res){
        console.log('fail',res)
      },
    }
  },
});
